//
// Created by ZLY on 2025/5/11.
//

#include "weapon_thunder.h"

#include "core/actor.h"
#include "core/scene.h"
#include "world/spell.h"

void WeaponThunder::init() {
    Weapon::init();
    auto scene = game_.getCurrentScene();
    auto pos = glm::vec2(game_.getScreenSize().x - 300, 30);
    hud_skill_ = HUDSkill::addHUDSkillChild(scene, "resources/UI/Electric-Icon.png", pos, 0.14f,
                                            Anchor::CENTER);
}

void WeaponThunder::update(float dt) {
    Weapon::update(dt);
    if (hud_skill_) {
        hud_skill_->setPercent(cooldown_timer_ / cooldown_);
    }
}

WeaponThunder *WeaponThunder::addWeaponThunderChild(Actor *parent, float cooldown, float mana_cost) {
    auto weapon_thunder = new WeaponThunder();
    weapon_thunder->init();
    weapon_thunder->setParent(parent);
    weapon_thunder->setCooldown(cooldown);
    weapon_thunder->setManaCost(mana_cost);
    if (parent) {
        parent->addChild(weapon_thunder);
    }
    return weapon_thunder;
}

bool WeaponThunder::handleEvents(SDL_Event &event) {
    if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
        if ((event.button.button = SDL_BUTTON_LEFT)) {
            if (canAttack()) {
                game_.playSound("resources/sound/big-thunder.mp3");
                auto pos = game_.getMousePosition() + game_.getCurrentScene()->getCameraPos();
                auto spell = Spell::addSpellChild(nullptr, "resources/effect/Thunderstrike w blur.png",
                                                  pos, 40.0f, 3.0f, Anchor::CENTER);
                attack(pos, spell);
                return true;
            }
        }
    }
    return false;
}
